#include "webpageadapter.h"

WebPageAdapter::WebPageAdapter(WebPage* webpage, PageProtocol *client, WebSpiderRequest *req, QObject *parent)
    : QObject(parent)
    , m_page(webpage)
    , m_client(client)
    , m_operator(new Operator(webpage, req, this))
    , m_logger(Log4Qt::Logger::logger("WebPageAdapter"))
{
    connect(m_page, SIGNAL(initialized()), SLOT(onInitialized())); 
    connect(m_page, SIGNAL(loadStarted()), SLOT(onLoadStarted())); 
    connect(m_page, SIGNAL(loadFinished(QString)), SLOT(onLoadFinished(QString))); 
    connect(m_page, SIGNAL(javaScriptAlertSent(QString)), SLOT(onJavaScriptAlertSent(QString))); 
    connect(m_page, SIGNAL(javaScriptConsoleMessageSent(QString)), SLOT(onJavaScriptConsoleMessageSent(QString))); 
    connect(m_page, SIGNAL(javaScriptErrorSent(QString, QString)), SLOT(onJavaScriptErrorSent(QString, QString))); 
    connect(m_page, SIGNAL(resourceRequested(QVariant, QObject*)), SLOT(onResourceRequested(QVariant, QObject*))); 
    connect(m_page, SIGNAL(resourceReceived(QVariant)), SLOT(onResourceReceived(QVariant))); 
    connect(m_page, SIGNAL(resourceError(QVariant)), SLOT(onResourceError(QVariant))); 
    connect(m_page, SIGNAL(resourceTimeout(QVariant)), SLOT(onResourceTimeout(QVariant))); 
    connect(m_page, SIGNAL(urlChanged(QUrl)), SLOT(onUrlChanged(QUrl))); 
    connect(m_page, SIGNAL(navigationRequested(QUrl, QString, bool, bool)), SLOT(onNavigationRequested(QUrl, QString, bool, bool))); 
    connect(m_page, SIGNAL(rawPageCreated(QObject*)), SLOT(onRawPageCreated(QObject*))); 
    connect(m_page, SIGNAL(closing(QObject*)), SLOT(onClosing(QObject*))); 
    connect(m_operator, SIGNAL(didSelfDestroyParse()), SLOT(onSelfDestroyParse()));
    connect(m_operator, SIGNAL(didTimeout()), SLOT(onTimeout()));
}

WebPageAdapter::~WebPageAdapter()
{
}

void WebPageAdapter::openUrl(const QString &address, const QVariant &op, const QVariantMap &settings, const QVariantList &cookie)
{
    if (m_page) {
        m_page->openUrl(address, op, settings, cookie);
        m_operator->startTimeoutTimer();
    }
}

void WebPageAdapter::stop()
{
    if (m_page)
        m_page->stop();
}

void WebPageAdapter::release()
{
    m_operator->release();
    m_operator = NULL;

    m_page->release();
    m_page = NULL;

    m_client = NULL;

    deleteLater();
}

Operator* WebPageAdapter::opt()
{
    return m_operator;
}

WebPage* WebPageAdapter::page()
{
    return m_page;
}

// private slots
void WebPageAdapter::onInitialized()
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didInitialized(m_operator);
    m_logger->debug("end");
}

void WebPageAdapter::onLoadStarted()
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didLoadStarted(m_operator);
    m_logger->debug("end");
}

void WebPageAdapter::onLoadFinished(const QString &status)
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didLoadFinished(m_operator, status);
    m_logger->debug("end");
}

void WebPageAdapter::onJavaScriptAlertSent(const QString &msg)
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didJavaScriptAlertSent(m_operator, msg);
    m_logger->debug("end");
}

void WebPageAdapter::onJavaScriptConsoleMessageSent(const QString &message)
{
    if (!m_client)
        return ;

    // TODO: Do monitor update pull
    //m_client->didJavaScriptConsoleMessageSent(m_operator, message);
}

void WebPageAdapter::onJavaScriptErrorSent(const QString &msg, const QString &stack)
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didJavaScriptErrorSent(m_operator, msg, stack);
    m_logger->debug("end");
}

void WebPageAdapter::onResourceRequested(const QVariant &requestData, QObject *request)
{
    if (!m_client)
        return ;

    // TODO: Do monitor update pull
    m_client->didResourceRequested(m_operator, requestData, request);
}

void WebPageAdapter::onResourceReceived(const QVariant &resource)
{
    if (!m_client)
        return ;

    // TODO: Do monitor update pull
    m_client->didResourceReceived(m_operator, resource);
}

void WebPageAdapter::onResourceError(const QVariant &errorData)
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didResourceError(m_operator, errorData);
    m_logger->debug("end");
}

void WebPageAdapter::onResourceTimeout(const QVariant &errorData)
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didResourceTimeout(m_operator, errorData);
    m_logger->debug("end");
}

void WebPageAdapter::onUrlChanged(const QUrl &url)
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didUrlChanged(m_operator, url);
    m_logger->debug("end");
}

void WebPageAdapter::onNavigationRequested(const QUrl &url, const QString &navigationType, bool navigationLocked, bool isMainFrame)
{
    if (!m_client)
        return ;

    // TODO: Do monitor update pull
    //m_client->didNavigationRequested(m_operator, url, navigationType, navigationLocked, isMainFrame);
}

void WebPageAdapter::onRawPageCreated(QObject *page)
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didRawPageCreated(m_operator, page);
    m_logger->debug("end");
}

void WebPageAdapter::onClosing(QObject *page)
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    m_client->didClosing(m_operator, page);
    m_logger->debug("end");
}

void WebPageAdapter::onSelfDestroyParse()
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    QString status(SET_SELF_DESTROY_STATUS);
    m_client->didSelfDestroyParse(m_operator, status);
    m_logger->debug("end");
}

void WebPageAdapter::onTimeout()
{
    if (!m_client)
        return ;

    m_logger->debug(__func__);
    // TODO: Do monitor update pull
    QString status(SET_TIMEOUT_STATUS);
    m_client->didTimeout(m_operator, status);
    m_logger->debug("end");
}

